package cn.nbt.busManage.controller;

import cn.nbt.busManage.common.ResponseResult;
import cn.nbt.busManage.model.dto.NewsDto;
import cn.nbt.busManage.model.dto.RoleDto;
import cn.nbt.busManage.model.vo.NewsVo;
import cn.nbt.busManage.model.vo.RoleVo;
import cn.nbt.busManage.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleService roleService;


    /**
     * 查询所有角色信息
     * @return
     */
    @GetMapping("/list")
    public ResponseResult<Object>list(){
        ResponseResult<Object> result = new ResponseResult<>();
        try{
            List<RoleDto>roleDtoList = roleService.findAll();
            if(roleDtoList == null){
                result.Error();
                return result;
            }
            ArrayList<RoleVo>roleVos = new ArrayList<>();
            for (RoleDto roleDto : roleDtoList){
                roleVos.add(new RoleVo(roleDto));
            }
            result.Success("查询成功", roleDtoList);
        }catch (Exception e){
            e.fillInStackTrace();
            result.Error("查询失败");
        }
        return result;
    }

    /**
     * 根据角色名称查询
     * @param roleName
     * @return
     */
    @GetMapping("/findByQuery")
    public ResponseResult<Object>findByQuery(@RequestParam(name = "roleName")String roleName){
        ResponseResult<Object>result=new ResponseResult<>();
        try{
            List<RoleDto> byQuery = roleService.findByQuery(roleName);
            if (byQuery.isEmpty()){
                result.BadRequest();
                return result;
            }
            ArrayList<RoleVo> roleVos = new ArrayList<>();
            for (RoleDto roleDto : byQuery){
                RoleVo roleVo = new RoleVo(roleDto);
                roleVos.add(roleVo);
            }
            result.Success("查询成功",roleVos);
        }catch(Exception e){
            e.fillInStackTrace();
            result.Error("查询失败");
        }
        return result;
    }

    /**
     * 删除角色信息
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    public ResponseResult<Object> delete(@RequestParam(name = "id") Integer id){
        ResponseResult<Object> result = new ResponseResult<>();
        try{
            roleService.delete(id);
            result.Success("删除成功");
        }catch (Exception e){
            e.fillInStackTrace();
            result.Error("删除失败");
        }
        return result;
    }

    /**
     * 添加角色信息
     * @param roleVo
     * @return
     */
    @PostMapping("/add")
    public ResponseResult<Object> add(@RequestBody RoleVo roleVo){
        ResponseResult<Object> result = new ResponseResult<>();
        try{
            RoleDto roleDto = new RoleDto(roleVo);
            roleService.add(roleDto);
            result.Success("添加成功");
        }catch (Exception e){
            e.fillInStackTrace();
            result.Error("添加失败");
        }
        return result;
    }

    /**
     * 更新角色信息
     * @param roleVo
     * @return
     */
    @PostMapping("/update")
    public ResponseResult<Object> update(@RequestBody RoleVo roleVo){
        ResponseResult<Object>result = new ResponseResult<>();
        try{
            RoleDto roleDto = new RoleDto(roleVo);
            roleService.update(roleDto);
            result.Success("更新成功");
        }catch (Exception e){
            e.fillInStackTrace();
            result.Error("更新失败");
        }
        return result;
    }
    /**
     * 根据id查询数据
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public ResponseResult<Object> findById (@RequestParam(name="id") Integer id){
        ResponseResult <Object> result=new ResponseResult<>();
        try {
            RoleDto dto = roleService.findById(id);
            if (dto == null){
                result.BadRequest();
                return result;
            }
            RoleVo roleVo = new RoleVo(dto);
            result.Success("查询成功",roleVo);
        }catch (Exception e){
            e.fillInStackTrace();
            result.Error("查询失败");
        }
        return result;
    }
}
